﻿using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Model;
using Minecraft.Model.Backpack;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Minecraft.ServerHall.Cmd
{
	public class EquipmentGoodsSattireIn : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Equipment_EquipmentGoodsSattireIn;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<EquipmentGoodsSattireInReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未登录）" });
				return;
			}
			session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：接口已经废弃，请使用接口：{EnumCommand.Backpack_BackpackGoodsExchange.ToString()}）" });
			return;
			//if (JsonConfig.Value.IsDebug)
			//{
			//	req = new EquipmentGoodsSattireInReq
			//	{
			//		enumEquipmentGoodsBelongsToType = EnumGoodsBelongsToType.SurvivorEquipment,
			//		enumGoodsBelongsToType = EnumGoodsBelongsToType.Backpack,
			//		goodsId = (from info in session.minecraftSessionInfo.goodsList
			//				   where (info.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.ShortcutBar || info.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.Backpack)
			//				   && info.GoodsItemId == "120000"
			//				   select info).ToList().FirstOrDefault().GoodsId,
			//		guid = ""
			//	};
			//}

			//物品来源判断
			if (!MinecraftConfig.bpShortbarBelongsToTypeList.Any(m => m == req.enumGoodsBelongsToTypeSource))
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：物品来源不正确，前端传入的物品来源为【{req.enumGoodsBelongsToTypeSource.ToString()}】）" });
				return;
			}
			//装备类型判断
			if (!MinecraftConfig.equipmentBelongsToTypeList.Any(m => m == req.enumGoodsBelongsToTypeTarget))
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：装备类型不正确，前端传入的装备类型为【{req.enumGoodsBelongsToTypeSource.ToString()}】）" });
				return;
			}
			if (req.GoodsPositionSource < 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误4：（原因：GoodsPositionSource < 0）" });
				return;
			}
			if (req.GoodsPositionTarget < 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误5：（原因：GoodsPositionTarget < 0）" });
				return;
			}
			if (req.guid.IsNullOrWhiteSpace())
			{
				req.guid = "";
			}

			////暂时写死
			//if (req.enumGoodsBelongsToTypeTarget == EnumGoodsBelongsToType.SurvivorEquipment)
			//{
			//	req.guid = session.minecraftSessionInfo.survivorList.FirstOrDefault(m => m.PlayerId == session.minecraftSessionInfo.player.PlayerId).SurvivorId;
			//}

			//if (req.goodsId.IsNullOrWhiteSpace())
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：goodsId参数不能为空）" });
			//	return;
			//}

			var goodsBelongsTypeList = new List<EnumGoodsBelongsToType>();
			if (MinecraftConfig.bpShortbarBelongsToTypeList.Any(m => m == req.enumGoodsBelongsToTypeSource))
			{
				goodsBelongsTypeList = MinecraftConfig.bpShortbarBelongsToTypeList;
			}
			else
			{
				goodsBelongsTypeList = new List<EnumGoodsBelongsToType>() { req.enumGoodsBelongsToTypeSource };
			}

			var goodsInfo = session.minecraftSessionInfo.goodsList.FirstOrDefault(m =>
				goodsBelongsTypeList.Any(k => k == m.enumGoodsBelongsToType_Convert)
				&& m.GoodsPosition==req.GoodsPositionSource);
			if (goodsInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：该物品不存在于【{goodsBelongsTypeList.JsonSerialize()}】中，或者根本就不存在）" });
				return;
			}

			//var mod_Items = CsvConfig.itemsList.FirstOrDefault(m => m.Id == goodsInfo.GoodsItemId);
			var mod_Items = CsvConfig.GetMod_ItemsInfoById(goodsInfo.GoodsItemId);
			if (mod_Items == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：物品配置表中不存在）" });
				return;
			}
			if (mod_Items.enumGoodsType_Convert != EnumGoodsType.Equipment)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：该物品不属于装备，目前该物品类型为【{mod_Items.enumGoodsType_Convert.ToString()}】）" });
				return;
			}
			//Mod_Player_Parts mod_Player_Parts = CsvConfig.player_partsList.FirstOrDefault(m => m.ID == goodsInfo.GoodsItemId);
			Mod_Player_Parts mod_Player_Parts = CsvConfig.GetMod_Player_PartsInfoById(goodsInfo.GoodsItemId);
			if (mod_Player_Parts == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：装备配置表中不存在该物品）" });
				return;
			}

			var equipment = session.minecraftSessionInfo.goodsList.FirstOrDefault(m =>
				m.enumGoodsBelongsToType_Convert == req.enumGoodsBelongsToTypeTarget && m.guid == req.guid
				&& m.GoodsItemId == goodsInfo.GoodsItemId
				&& m.GoodsPosition == mod_Player_Parts.Position_Convert);

			if (equipment != null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：该装备不能重复穿戴）" });
				return;
			}

			goodsInfo.Num--;

			//session.minecraftSessionInfo.goodsList.RemoveAll(m => m.GoodsId == goodsInfo.GoodsId);

			session.minecraftSessionInfo.goodsList.Add(new GoodsModel
			{
				BelongsTo = (int)req.enumGoodsBelongsToTypeTarget,
				GoodsId = StringHelper.GetGuidStr(),
				PlayerId = session.minecraftSessionInfo.player.PlayerId,
				WastageValue = goodsInfo.WastageValue,
				GoodsItemId = goodsInfo.GoodsItemId,
				GoodsPosition = mod_Player_Parts.Position_Convert,
				guid = req.guid,
				Num = 1,
			});

			EnumAttribute enumAttribute = mod_Items.modAttributeInfo_Convert.enumAttribute;
			int attributeAfterVal = 0;
			switch (mod_Items.modAttributeInfo_Convert.enumAttribute)
			{
				case EnumAttribute.Def:
					{
						switch (req.enumGoodsBelongsToTypeTarget)
						{

							case EnumGoodsBelongsToType.Equipment:
								{
									session.minecraftSessionInfo.player.Fight_Defense += mod_Items.modAttributeInfo_Convert.Value;
									attributeAfterVal = session.minecraftSessionInfo.player.Fight_Defense;
								}
								break;
							case EnumGoodsBelongsToType.SurvivorEquipment:
								{
									//幸存者装备
								}
								break;
							default:
								break;
						}


					}
					break;
				case EnumAttribute.BagSize:
					{
						switch (req.enumGoodsBelongsToTypeTarget)
						{
							case EnumGoodsBelongsToType.Equipment:
								{
									session.minecraftSessionInfo.BackpackCellNum += mod_Items.modAttributeInfo_Convert.Value;
									attributeAfterVal = session.minecraftSessionInfo.BackpackCellNum;
								}
								break;
							case EnumGoodsBelongsToType.SurvivorEquipment:
								{
									//幸存者装备
								}
								break;
							default:
								break;
						}
					}
					break;
				default:
					{
						session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：装备穿戴中，暂不支持该属性【{mod_Items.modAttributeInfo_Convert.Id}:{mod_Items.modAttributeInfo_Convert.enumAttribute.ToString()}】加点）" });
						return;
					}
			}

			//添加操作影响的positionOptList
			List<BackpackGoodsPositionOpt> positionOptList = new List<BackpackGoodsPositionOpt>();
			positionOptList.Add(new BackpackGoodsPositionOpt
			{
				enumGoodsBelongsToType = goodsInfo.enumGoodsBelongsToType_Convert,
				GoodsPosition = goodsInfo.GoodsPosition,
				guid = goodsInfo.guid
			});
			positionOptList.Add(new BackpackGoodsPositionOpt
			{
				enumGoodsBelongsToType = req.enumGoodsBelongsToTypeTarget,
				GoodsPosition = mod_Player_Parts.Position_Convert,
				guid = req.guid
			});

			//组装响应数据
			List<BackpackCellInfo> posGoodsinfoList = new List<BackpackCellInfo>();
			//获取影响的位置列表（只有两个，一个背包，一个家具）
			positionOptList.ForEach(optItem =>
			{
				//找到受影响的物品位置信息
				posGoodsinfoList = session.minecraftSessionInfo.goodsList.Where(m => m.enumGoodsBelongsToType_Convert == optItem.enumGoodsBelongsToType && m.GoodsPosition == optItem.GoodsPosition && m.guid == req.guid).GetBackpackGoodsCellInfo();
			});
			//if (posGoodsinfoList.Count() == 1)
			//{
			//	posGoodsinfoList.Add(new BackpackCellInfo
			//	{
			//		enumGoodsBelongsToType = goodsInfo.enumGoodsBelongsToType_Convert,
			//		GoodsItemId = goodsInfo.GoodsItemId,
			//		GoodsList = new List<BackpackGoodsInfo>(),
			//		GoodsPosition = goodsInfo.GoodsPosition,
			//		WastageValue = goodsInfo.WastageValue,
			//		guid = req.guid
			//	});
			//}

			session.minecraftSessionInfo.goodsList.RemoveAll(m => m.Num <= 0);

			var resp = new EquipmentGoodsSattireInResp
			{
				posGoodsinfoList = posGoodsinfoList,
				enumAttribute = enumAttribute,
				attributeAfterVal = attributeAfterVal,
				enumEquipmentGoodsBelongsToType = req.enumGoodsBelongsToTypeTarget,
				guid = req.guid
			};
			session.Send(defCommand, req, resp);
		}
	}
}
